<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Coupon_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
	}

	function pending(){
		$total = count( $this->db->where('validity','pending')->get(TBL_COUPON)->result() );
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/coupon/pending/?", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
							
		$records = $this->db->where('validity','pending')->limit($records_per_page, $per_page )->get(TBL_COUPON)->result();
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	function redeem(){
		$total = count( $this->db->where('validity','redeem')->get(TBL_COUPON)->result() );
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/coupon/redeem/?", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
							
		$records = $this->db->where('validity','redeem')->limit($records_per_page, $per_page )->get(TBL_COUPON)->result();
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	function coupon_submit(){
		$id = $this->input->post('id', TRUE);
		if(!$id){
			$startTime =  convertToTimeStampFromFormat($_POST['start_time']);
			$endTime = convertToTimeStampFromFormat($_POST['end_time']);
			$mode = $_POST['mode'];
			$code = $_POST['code'];
			$coin = $_POST['coin'];
			$COUPON = $this->coupon_entity->init('code',$code);
			if($COUPON){
				echo "Code is already existed.";
				exit();
			}
			if(! $this->phpvalidator->is_natural($coin) OR $coin <= 0){
				echo "Coin is not valid.";
				exit();
			}
			if( $startTime == 0 OR $endTime == 0 ){
				echo "Datetime not validated.";exit();
			}
			if($endTime < $startTime){
				echo "Datetime not validated.";exit();
			}
			
			$newId = $this->coupon_entity->createNewEntity();
			$COUPON = $this->coupon_entity->init('id',$newId);
			$COUPON->setCode($code);
			$COUPON->setCoin($coin);
			$COUPON->setValidity('pending');
			$COUPON->setStartDate($startTime);
			$COUPON->setEndDate($endTime);
			$COUPON->setCreateDate(time());
			$COUPON->setMode($mode);
			$COUPON->setUserRedeem(NULL);
			$COUPON->update();
		}else{
			$startTime =  convertToTimeStampFromFormat($_POST['start_time']);
			$endTime = convertToTimeStampFromFormat($_POST['end_time']);
			$mode = $_POST['mode'];
			$code = $_POST['code'];
			$coin = $_POST['coin'];
			$COUPON = clone_( $this->coupon_entity->init('code',$code) );
			if($COUPON AND $id != $COUPON->getId()){
				echo "Code is already existed.";
				exit();
			}
			if(! $this->phpvalidator->is_natural($coin) OR $coin <= 0){
				echo "Coin is not valid.";
				exit();
			}
			if( $startTime == 0 OR $endTime == 0 ){
				echo "Datetime not validated.";exit();
			}
			if($endTime < $startTime){
				echo "Datetime not validated.";exit();
			}
			
			$COUPON = $this->coupon_entity->init('id',$id);
			$COUPON->setCode($code);
			$COUPON->setCoin($coin);
			$COUPON->setStartDate($startTime);
			$COUPON->setEndDate($endTime);
			$COUPON->setCreateDate(time());
			$COUPON->setMode($mode);
			$COUPON->update();
		}
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	function delete_coupon(){
		$id = $_POST['id'];
		$this->db->where('id',$id)->delete(TBL_COUPON);
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'ok';exit();
	}
	
	
	//end class
}
